package com.example.deptempmanageservice.controller;

import com.myprojects.common.pojo.Department;
import com.example.deptempmanageservice.service.DepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import com.myprojects.common.dto.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/department-curd")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    /**
     * 该方法用于分页查询所有的部门信息
     */
    @RequestMapping(value = "/departments", method = RequestMethod.GET)
    @ResponseBody
    public Msg getAllDepartments() {
        Msg result;//要返回的结果
        try {
            List<Department> departmentList = departmentService.findAllDepartment();
            //生成返回成功的结果
            result = Msg.succeed();
            //将departments加入返回的结果中
            result.addData("departments", departmentList);
            System.out.println(result.getDataMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = Msg.failed();
            result.setMsg("服务端处理异常!");
        }
        return result;
    }

    /**
     * 该方法用于分页查询所有的部门信息
     */
    @RequestMapping(value = "/department", method = RequestMethod.GET)
    @ResponseBody
    public Msg selectDepartmentByPageNum(
            @RequestParam(value = "pn", defaultValue = "1") Integer pageNum) {//普通
        Msg result;//要返回的结果
        try {
            //在查询之前调用，要设置查询的页数和每页的大小
            PageHelper.startPage(pageNum, 10);
            //startPage后紧跟的查询就是一个分页查询
            List<Department> departmentList = departmentService.findAllDepartment();
            //使用PageInfo包装查询后的结果，需要将pageInfo交给页面
            //将查询出来用户信息加入pageInfo，并连续显示5页
            PageInfo pageInfo = new PageInfo(departmentList, 5);
            //生成返回成功的结果
            result = Msg.succeed();
            //将pageInfo加入返回的结果中
            result.addData("pageInfo", pageInfo);
            System.out.println(result.getDataMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = Msg.failed();
            result.setMsg("服务端处理异常!");
        }

        return result;
    }

    /**
     * 该方法用于保存部门的信息
     */
    @RequestMapping(value = "/department", method = RequestMethod.POST)
    @ResponseBody
    public Msg saveDepartment(@Valid Department department, BindingResult result) {
        Msg msg = null;

        //先检查部门信息有没有错误
        Map<String, Object> errorMap = departmentService.checkDepartment(department, result);

        //如果都无误，则尝试保存
        if (errorMap.size() < 1) {
            int iSaveResult = departmentService.insertSelective(department);
            if (iSaveResult > 0) {
                msg = Msg.succeed();
                msg.setMsg("保存部门信息成功！");
            } else {
                msg = Msg.failed();
                msg.setMsg("保存部门信息失败！");
            }
        } else {
            msg = Msg.failed();
            msg.setMsg("保存部门信息失败！");
            //将详细错误信息添加进msg
            msg.addData("fieldErrors", errorMap);
        }

        return msg;
    }

    /**
     * 该方法用于一次删除多个部门
     */
    @RequestMapping(value = "/department", method = RequestMethod.DELETE)
    @ResponseBody
    public Msg deleteLotsDepartment(@RequestParam List<Integer> idList) {
        Msg msg = null;

        int iResult = departmentService.deleteLotsByPrimaryKey(idList);
        if (iResult > 0) {
            msg = Msg.succeed();
            msg.setMsg("删除成功，共删除" + iResult + "个部门");
        } else {
            msg = Msg.failed();
            msg.setMsg("删除失败!");
        }
        return msg;
    }


    /**
     * 该方法用更新于一个部门
     */
    @RequestMapping(value = "/department", method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateDepartment(@Valid @RequestBody Department department, BindingResult result) {
        Msg msg = null;

        //先检查部门信息有没有错误
//        Map<String,Object> errorMap = departmentService.checkUser(department,result);

        //如果都无误，则尝试保存
        int iResult = departmentService.updateByPrimaryKeySelective(department);
        if (iResult > 0) {
            msg = Msg.succeed();
            msg.setMsg("更新成功，共更新" + iResult + "个部门");
        } else {
            msg = Msg.failed();
            msg.setMsg("更新失败!");
        }

        return msg;
    }

}
